package net.googlecode.harchenko.task5;

import net.googlecode.harchenko.task5.model.Manager;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

/**
 * Class, that realize Task5.
 */
public class Task5 {

    /**
     * Class-comparator to compare managers to Last name.
     */
    private class SalaryComparator implements Comparator<Manager> {
        @Override
        public int compare(Manager o1, Manager o2) {
            return o1.getLastName().compareTo(o2.getLastName());
        }
    }

    /**
     * Sorts input ArrayList to personal code, delete persons with minimum
     * and maximum salary and prints report.
     *
     * @param lst Input ArrayList
     */
    public void codeSort(final ArrayList<Manager> lst) {


        Collections.sort(lst,new SalaryComparator());

        lst.remove(0);
        lst.remove(lst.size() - 1);
        Collections.sort(lst, new Comparator<Manager>() {
            @Override
            public int compare(Manager o1, Manager o2) {
              return (o1.getId()<o2.getId() ? -1 : (o1.getId()==o2.getId() ? 0 : 1));
            }
        });
        for (Manager manager : lst) {
            System.out.println(manager);
        }

    }


    /** Sorts input ArrayList to Last name and prints report.
    * @param lst Input ArrayList
    */
    public void nameSort(final ArrayList<Manager> lst) {

        Collections.sort(lst, new Comparator<Manager>() {
            @Override
            public int compare(Manager o1, Manager o2) {

                return o1.getLastName().compareTo(o2.getLastName());
            }
        });

        for (Manager manager : lst) {
            if (manager.getAge() < 45) {
                System.out.println(manager);
            }
        }

    }

    /**
     * Sorts input ArrayList to personal salary and prints report.
     *
     * @param lst Input ArrayList
     */

    public void salarySort(final ArrayList<Manager> lst) {
        Collections.sort(lst, new SalaryComparator());

        BigDecimal prevsalary = new BigDecimal(-1);

        for (Manager x : lst) {
            if (x.getSalary().compareTo(prevsalary) != 0) {
                System.out.println(x);
                prevsalary = x.getSalary();
            }
        }
    }
}
